package com.commerce.common.message;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 秒杀消息实体
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FlashSaleMessage implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 消息ID
     */
    private String messageId;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 秒杀活动ID
     */
    private Long flashSaleId;

    /**
     * 商品ID
     */
    private Long productId;

    /**
     * 商品名称
     */
    private String productName;

    /**
     * 商品图片
     */
    private String productImage;

    /**
     * 秒杀价格
     */
    private BigDecimal flashPrice;

    /**
     * 购买数量
     */
    private Integer quantity;

    /**
     * 用户IP
     */
    private String userIp;

    /**
     * 用户设备信息
     */
    private String userAgent;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 消息类型：ORDER-订单创建，ROLLBACK-库存回滚
     */
    private String messageType;

    /**
     * 重试次数
     */
    private Integer retryCount;

    /**
     * 最大重试次数
     */
    private Integer maxRetryCount;

    /**
     * 订单号（订单创建成功后设置）
     */
    private String orderNo;

    /**
     * 处理状态：PENDING-待处理，SUCCESS-成功，FAILED-失败
     */
    private String status;

    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 扩展字段
     */
    private String extData;

    public FlashSaleMessage(Long userId, Long flashSaleId, Long productId, 
                           String productName, String productImage, BigDecimal flashPrice, 
                           Integer quantity, String userIp, String userAgent) {
        this.messageId = generateMessageId();
        this.userId = userId;
        this.flashSaleId = flashSaleId;
        this.productId = productId;
        this.productName = productName;
        this.productImage = productImage;
        this.flashPrice = flashPrice;
        this.quantity = quantity;
        this.userIp = userIp;
        this.userAgent = userAgent;
        this.createTime = LocalDateTime.now();
        this.messageType = "ORDER";
        this.retryCount = 0;
        this.maxRetryCount = 3;
        this.status = "PENDING";
    }

    /**
     * 生成消息ID
     */
    private String generateMessageId() {
        return "FS_" + System.currentTimeMillis() + "_" + 
               (int)(Math.random() * 10000);
    }

    /**
     * 增加重试次数
     */
    public void incrementRetryCount() {
        this.retryCount = (this.retryCount == null ? 0 : this.retryCount) + 1;
    }

    /**
     * 是否超过最大重试次数
     */
    public boolean isMaxRetryExceeded() {
        return this.retryCount != null && this.maxRetryCount != null && 
               this.retryCount >= this.maxRetryCount;
    }

    /**
     * 设置成功状态
     */
    public void setSuccess(String orderNo) {
        this.status = "SUCCESS";
        this.orderNo = orderNo;
        this.errorMessage = null;
    }

    /**
     * 设置失败状态
     */
    public void setFailed(String errorMessage) {
        this.status = "FAILED";
        this.errorMessage = errorMessage;
    }
}
